<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<title>注册 - 从零开始 ToZero</title>

		<meta name="description" content="User login page" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />

		<!-- basic styles -->

		<link href="__PUBLIC__/assets/css/bootstrap.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="__PUBLIC__/assets/css/font-awesome.min.css" />

		<!--[if IE 7]>
		  <link rel="stylesheet" href="__PUBLIC__/assets/css/font-awesome-ie7.min.css" />
		<![endif]-->

		<!-- page specific plugin styles -->

		<!-- fonts -->

		<link rel="stylesheet" href="__PUBLIC__/assets/css/ace-fonts.css" />

		<!-- ace styles -->

		<link rel="stylesheet" href="__PUBLIC__/assets/css/ace.min.css" />
		<link rel="stylesheet" href="__PUBLIC__/assets/css/ace-rtl.min.css" />

		<!--[if lte IE 8]>
		  <link rel="stylesheet" href="__PUBLIC__/assets/css/ace-ie.min.css" />
		<![endif]-->

		<!-- inline styles related to this page -->

		<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->

		<!--[if lt IE 9]>
		<script src="__PUBLIC__/assets/js/html5shiv.js"></script>
		<script src="__PUBLIC__/assets/js/respond.min.js"></script>
		<![endif]-->
	</head>

	<body class="login-layout">
		<div class="main-container">
			<div class="main-content">
				<div class="row">
					<div class="col-sm-10 col-sm-offset-1">
						<div class="login-container">
							<div class="center">
								<h1>
									<i class="icon-leaf green"></i>
									<span class="red">从零开始</span>
									<span class="white">To Zero</span>
								</h1>
								<h4 class="blue">&copy; 广州训练营</h4>
							</div>

							<div class="space-6"></div>
							<div class="position-relative">
							<!-- 注册 -->
								<div id="signup-box" class="signup-box widget-box no-border">
									<div class="widget-body">
										<div class="widget-main">
											<h4 class="header green lighter bigger">
												<i class="icon-group blue"></i>
												注册新用户
											</h4>

											<div class="space-6"></div>
											<p> Enter your details to begin: </p>

											<form>
												<fieldset>
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="email" class="form-control" placeholder="用户名" name="username" min="6" />
															<i class="icon-user"></i>
														</span>
													</label>

													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input name="password" type="password" class="form-control" placeholder="密码" />
															<i class="icon-lock"></i>
														</span>
													</label>

													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="password" class="form-control" name="repassword" placeholder="重复确认密码" />
															<i class="icon-retweet"></i>
														</span>
													</label>
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="number" name="phone" class="form-control" placeholder="手机号码" />
															<i class="icon-phone"></i>
														</span>
													</label>
													<label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="number" name="verify" class="form-control width-50 pull-left" placeholder="输入手机验证短信" />
															<!-- <i class="icon-retweet"></i> -->
															<button id="get_alidy" class="width-50 pull-right btn btn-sm btn-primary" type="button">获取验证码</button>
														</span>	
													</label>
													<label class="block">
														<input type="checkbox" class="ace" name="agree" checked="true" />
														<span class="lbl">
															同意
															<a href="#">用户守则</a>
														</span>
													</label>

													<div class="space-24"></div>

													<div class="clearfix">
														<button type="reset" class="width-30 pull-left btn btn-sm">
															<i class="icon-refresh"></i>
															重置
														</button>

														<button type="button" id="reg_submit" class="width-65 pull-right btn btn-sm btn-success">
															注册
															<i class="icon-arrow-right icon-on-right"></i>
														</button>
													</div>
												</fieldset>
											</form>
										</div>

										<div class="toolbar center">
											<a href="#" onclick="show_box('login-box'); return false;" class="back-to-login-link">
												<i class="icon-arrow-left"></i>
												已有账号，返回登入
											</a>
										</div>
									</div><!-- /widget-body -->
								</div><!-- /signup-box -->
							</div><!-- /position-relative -->
						</div>
					</div><!-- /.col -->
				</div><!-- /.row -->
			</div>
		</div><!-- /.main-container -->

		<!-- basic scripts -->

		<!--[if !IE]> -->

		<script type="text/javascript">
			window.jQuery || document.write("<script src='__PUBLIC__/assets/js/jquery-2.0.3.min.js'>"+"<"+"/script>");
		</script>

		<!-- <![endif]-->

		<!--[if IE]>
<script type="text/javascript">
 window.jQuery || document.write("<script src='__PUBLIC__/assets/js/jquery-1.10.2.min.js'>"+"<"+"/script>");
</script>
<![endif]-->

		<script type="text/javascript">
			if("ontouchend" in document) document.write("<script src='__PUBLIC__/assets/js/jquery.mobile.custom.min.js'>"+"<"+"/script>");
		</script>

		<!-- 该页所有的javascript代码 -->
		<script src='__PUBLIC__/dialog/layer/layer.js'></script>
		<script src='__PUBLIC__/dialog/dialog.js'></script>
		<script src='__PUBLIC__/common/common.js'></script>
		<script type="text/javascript">
			function show_box(id) {
			 jQuery('.widget-box.visible').removeClass('visible');
			 jQuery('#'+id).addClass('visible');
			}
			show_box('signup-box');

			/**
			 *  倒计时
			 */
			function djs(obj)
			{
				$("#get_alidy").removeClass("btn-primary");
				// var i = 60;
				var i = 10;

				var timer = setInterval(function(){

					obj.html( i + " 秒后可以再次获取");
					i--;
				// console.log(i)
					if (i < 0) {
						clearInterval(timer);
						obj.html( "获取验证码");
						$("#get_alidy").addClass("btn-primary");
						return;
					};
				}, 1000);
			}

			/**
			 * 正则匹配手机号码
			 */
			function checkTel(phone){
			    var isMob=/^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|15[012356789][0-9]{8}|18[02356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/;
			    
			    if(isMob.test(phone)){
			    	
			        return true;
			    } else{
			        return false;
			    }
	    	}

			/**
			 * 获取短信验证码
			 */
			$("#get_alidy").click(function(){
				// 这里用户名是否被注册
				var username = $("input[name='username']").val().trim();
				if (username == "") {return dialog.error("用户名不能为空");};

				// 验证两次密码是否一致
				var password = $("input[name='password']").val().trim();
				var repassword = $("input[name='repassword']").val().trim();
				if (!password || !repassword) { return dialog.error("密码不能为空"); };
				if (password != repassword) { return dialog.error("两次密码不一致"); };
				
				//查询手机
				var phone = $("input[name='phone']").val().trim();
				if (phone == "") {return dialog.error("手机号码不能为空");};
				if (!checkTel(phone)) {return dialog.error("手机号码格式不对！");};
				check_isReg("username="+username+"&phone="+phone);
				// return ;
				var obj = $(this);

				djs(obj);
			});

			/**
			 * 检测用户是否已经注册
			 */
			function check_isReg(data)
			{
				$.ajax({
					url: "{:U('Register/sendMsg')}",
					data: data,
					type: "POST",
					dataType: 'JSON',
					success:function(res){
						console.log(res);
						if (res == 1) {
							return dialog.error("用户名已存在");
						};
						if (res == 2) {
							return dialog.error("手机号码已经注册");
						};
						return true;
					}
				});
			}

			/**	
			 * 提交最后的注册	
			 * ***遗漏同意 遵守用户守则
			 */
			$("#reg_submit").bind("click", function(){
				// 用户守则
				if ($(":checked").attr("checked") == "undefined") {
					return dialog.error("不遵守守则，将不能注册账号");
				};

				var verify = $("input[name='verify']").val().trim();
				if (!verify) { return dialog.error("请填写验证码"); };
				// return alert(1);
				var data = $("form").serialize();
				var jump_url = "{:U('Login/login')}";
				$.ajax({
					url: "{:U('Register/doReg')}",
					data:data,
					dataType:"JSON",
					type:"POST",
					success:function(res){
						if (res.status == 1) {
							return dialog.success(res.message, jump_url);
						};
						return dialog.error(res.message);
					}
				});
			});
		</script>
	</body>
</html>
